package br.com.lopes.gci.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

public class NumeroTelefoneConverter implements Converter 
{

    public NumeroTelefoneConverter() 
    {
    }

    public Object getAsObject(FacesContext context, UIComponent component, String value)
    {       
        return getAsString(context, component, value);
    }

    public String getAsString(FacesContext context, UIComponent component, Object value)
    {
        String phoneNumber = null;

        if (value instanceof String && value != null && ((String)value).length() == 10)
        {
            phoneNumber = (String)value;

            StringBuilder phoneNumberAsString = new StringBuilder(phoneNumber);
            phoneNumberAsString.insert(0, "(");
            phoneNumberAsString.insert(3, ")");
            phoneNumberAsString.insert(4, " ");
            phoneNumberAsString.insert(9, "-");
            return phoneNumberAsString.toString();
        }
        return (String)value;
    } 
}

